package j.quartz.shiro.except;

import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 处理授权失败异常，跳转对应的异常页面（重定向到 controller）
 */
//@Component
public class JquartzExceptionResolver implements HandlerExceptionResolver {
  
    public ModelAndView resolveException(HttpServletRequest request,
                                         HttpServletResponse response, Object handler, Exception ex) {
        // TODO Auto-generated method stub  
        System.out.println("==============异常开始=============");  
        //如果是shiro无权操作，因为shiro 在操作auno等一部分不进行转发至无权限url  
        if(ex instanceof UnauthorizedException){
            // 重定向到请求
            ModelAndView mv = new ModelAndView("redirect:/unauthorizedUrl");
            return mv;  
        }
        ex.printStackTrace();  
        System.out.println("==============异常结束=============");  
        ModelAndView mv = new ModelAndView("error");  
        mv.addObject("exception", ex.toString().replaceAll("\n", "<br/>"));  
        return mv;  
    }  
  
}  